package user;

import java.sql.ResultSet;
import java.sql.SQLException;

public class Customer extends User {
	private String s;
	
	private String regName;
	private String realName;
	private String address;
	private String phone;
	private String email;
	
	public Customer() {
		super();
	}
	
	public Customer(String regName, String realName, String address, String phone, String email) {
		this.regName = regName;
		this.realName = realName;
		this.address = address;
		this.phone = phone;
		this.email = email;
	}
	
	public String getTruePassword() {	
		DBConnector db = DBConnector.getDBConnector();
		String sql = "select cus_passcode from customer where cus_login_name = '" + username + "';";
		
		ResultSet result = db.query(sql);
		try {
			if(result.next()) {
				return result.getString(1);
			} else
				return null;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * Get the customer profile according to the register name
	 * @param regName
	 * @return
	 */
	public static Customer profile(String regName) {
		DBConnector db = DBConnector.getDBConnector();
		String stmt = "select cus_name, cus_address, cus_phone, cus_email " 
					  + "from customer where cus_login_name = \'" 
					  + regName 
					  + "\'";
		
		ResultSet res = db.query(stmt);
		
		try {
			if (res.next()) {
				String name = res.getString(1);
				String address = res.getString(2);
				String phone = res.getString(3);
				String email = res.getString(4);
				
				return new Customer(regName, name, address, phone, email);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
		
	}
	
	public static void updateProfile(Customer customer) {
		DBConnector db = DBConnector.getDBConnector();
		String stmt = "update customer set " 
					  + " cus_name = \'" + customer.getRealName() + "\',"
					  + " cus_address = \'" + customer.getAddress() + "\',"
					  + " cus_phone = \'" + customer.getPhone() + "\',"
					  + " cus_email = \'" + customer.getEmail() + "\' " 
					  + " where cus_login_name = \'" + customer.getRegName() + "\'";
		db.update(stmt);
		
	}
	
	public void setRealName(String realName) {
		this.realName = realName;
	}
	
	public void setAddress(String address) {
		this.address = address;
	}
	
	public void setPhone(String phone) {
		this.phone = phone;
	}
	
	public void setEmail(String email) {
		this.email = email;
	}
	
	public String getRegName() {
		return regName;
	}
	
	public String getRealName() {
		return realName;
	}
	
	public String getAddress() {
		return address;
	}
	
	public String getPhone() {
		return phone;
	}
	
	public String getEmail() {
		return email;
	}
	
	
	public static String getCustomerList(String customer_search, String Order_By_CusName, String Order_By_CusAdd, String Order_By_CusEmail, String Order_By_CusPhone)
	{
		StringBuffer sBuf = new StringBuffer();
		DBConnector db = DBConnector.getDBConnector();
		String queryCustomer = "select cus_name, cus_address, cus_email, cus_phone from customer ";
	
		if (customer_search != null && customer_search.length() > 0)
			queryCustomer += "where (cus_name like '%" + customer_search
					+ "%' or cus_address like '%" + customer_search
					+ "%' or cus_email like '%" + customer_search + "%') ";
		if (Order_By_CusName != null && Order_By_CusName.length() > 0)
			queryCustomer += "order by cus_name ";
		else if (Order_By_CusAdd != null && Order_By_CusAdd.length() > 0)
			queryCustomer += "order by cus_address ";
		else if (Order_By_CusEmail != null && Order_By_CusEmail.length() > 0)
			queryCustomer += "order by cus_email desc";
		else if (Order_By_CusPhone != null && Order_By_CusPhone.length() > 0)
			queryCustomer += "order by cus_phone ";
	
		try{
			db.prepareStatement(queryCustomer);
			ResultSet queryCustomerResult = db.queryPstmt();
			if (queryCustomerResult != null) {
				sBuf.append("<tr bgColor=#cccccc><td>Customer Name</td><td>Customer Address</td><td>Customer Email</td><td>Customer Phone</td><td>Delete</td></tr>");
				if (queryCustomerResult.next()) {
					do {
						sBuf.append("<tr>");
						sBuf.append("<td>" + queryCustomerResult.getString(1)
								+ "</td>");
						sBuf.append("<td>" + queryCustomerResult.getString(2)
								+ "</td>");
						sBuf.append("<td>" + queryCustomerResult.getString(3)
								+ "</td>");
						sBuf.append("<td>" + queryCustomerResult.getString(4)
								+ "</td>");
						//hidden form store the name of the customer deleted
						sBuf.append("<form method='post' action='administrator_account.jsp' name = 'delete_customer'><div align='center'>");
						sBuf.append("<td><input name = 'customer_deleted' type = 'hidden' class = 'input' value = '" + queryCustomerResult.getString(1) + "'>");
						sBuf.append("<input name='DeleteCustomer' type='submit' value='Delete'></td></div></form></tr>");
					} while (queryCustomerResult.next());
				} else
					sBuf.append("<tr><td colspan = 5>There are no customer information fulfilling your requirement.</td></tr>");
			}
		}catch (Exception ex) {
			System.out.println("Customer.getCustomerList exception");
		}
		return sBuf.toString();
	}
}
